declare var tt;
declare var wx;
import { _decorator, AudioSourceComponent, Component, Node } from 'cc';
import { ResMgr } from './ResMgr';
import { Global } from '../Global';
const { ccclass, property } = _decorator;

@ccclass('AdioMgr')
/**
 * 音效管理
 */
export class AudioMgr {
    //
    private static _instance: AudioMgr = null;
    //
    public static get instance() {
        if (!this._instance) {
            this._instance = new AudioMgr();
        }
        return this._instance;
    }

    //背景音乐管理
    private BgSound: AudioSourceComponent = null;
    public IsBgPlay: boolean = true;

    //音效
    private Effect: AudioSourceComponent = null;
    public IsEffectPlay: boolean = true;

    //是否播放震动
    public IsShakePlay: boolean = true;


    /**
     * 初始化
     */
    public Init() {
        this.BgSound = new AudioSourceComponent();
        this.Effect = new AudioSourceComponent();
    }

    /**
     * 播放震动
     */
    public PlayShake() {
        if (!this.IsShakePlay) return;

        if (Global.IsDy) {//抖音播放震动
            tt.vibrateShort()
        }

        if (Global.IsWx) {//微信播放震动 heavy light
            wx.vibrateShort({
                type: "medium"
            })
        }

    }

    /**
     * 播放背景音效
     * @param 背景音效名称
     * @param 背景音效声量
     */
    public PlayBgSound(_bgsoundname: string, _volume: number = 0.6) {
        if (!this.IsBgPlay) return;

        this.BgSound.clip = ResMgr.Instance.Audio[_bgsoundname];
        this.BgSound.play();
        this.BgSound.volume = _volume;
        this.BgSound.loop = true;
    }

    /**
     * 背景音乐暂停
     */
    public StopBgSound() {
        this.BgSound.stop();
    }

    /**
     * 播放音效
     * @param 音效名称
     * @param 播放索引
     */
    public PlayEffect(_sound: string) {
        if (!this.IsEffectPlay) return;
        this.Effect.clip = ResMgr.Instance.Audio[_sound];
        this.Effect.loop = false;
        this.Effect.play();
    }

    /**
     * 暂停音效
     */
    public StopEffect() {
        this.Effect.stop();
    }
}

export const SoundName = {
    Bg: "bg",
    Click: "click",
    Choose: "upsound",
    Clear: "clear_music",
    Down: "click_music"
}


